package com.iflysse.services.impl;

import com.iflysse.common.RoleEnum;
import com.iflysse.dao.StudentDao;
import com.iflysse.dao.TeacherDao;
import com.iflysse.dao.UserDao;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.UserAccountIdAndPasswordBoReq;
import com.iflysse.pojo.bo.req.UserBoReq;
import com.iflysse.pojo.bo.req.UsernameAndEmailBoReq;
import com.iflysse.pojo.bo.resp.UserAccountIdBoResp;
import com.iflysse.pojo.bo.resp.UserBoResp;
import com.iflysse.pojo.bo.resp.UserTeacherInfoBoResp;
import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.StudentPoReq;
import com.iflysse.pojo.po.req.UpdateUserPwdPoReq;
import com.iflysse.pojo.po.req.UserIdPoReq;
import com.iflysse.pojo.po.req.UserPoReq;
import com.iflysse.pojo.po.resp.StudentPoResp;
import com.iflysse.pojo.po.resp.TeacherPoResp;
import com.iflysse.pojo.po.resp.UserAccountIdAndRolePoResp;
import com.iflysse.pojo.po.resp.UserPoResp;
import com.iflysse.services.UserService;
import com.iflysse.util.CommonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

@Transactional
@Service
public class UserServiceImpl implements UserService {

    public static final Logger log= Logger.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentDao studentDao;

    @Override
    public   UserBoResp login(UserBoReq userBoReq) throws SQLException {
        UserPoReq u = new UserPoReq();
        u.setUserAccountInfo(userBoReq.getUserName());
        u.setPassWord(userBoReq.getPassWord());
        UserBoResp ubr = null;
        UserPoResp upr = userDao.findUserByAccountInfoAndPassword(u);
        if(upr != null) {
            ubr = new UserBoResp();
            ubr.setUserId(upr.getAccountId());
            ubr.setRole(upr.getRole());
            //更新登录时间
            userDao.updateLoginDate(ubr.getUserId());
        }
        return ubr;
    }

    @Override
    public UserTeacherInfoBoResp queryIdByAccountIdAndRole(String accountId, String role)throws SQLException {
        //构建请求对象
        UserIdPoReq userIdPoReq =new UserIdPoReq();
        userIdPoReq.setAccountId(accountId);
        if(role.equals(RoleEnum.ROLETEACHER.getRoleId())){
            //查询老师
            TeacherPoResp teacherPoResp= teacherDao.getTeacherByAccountId(userIdPoReq);
            log.info("根据账户编号"+accountId+"查询的老师信息为："+teacherPoResp);
            if(teacherPoResp!=null){
                UserTeacherInfoBoResp userTeacherInfoBoResp=  CommonUtil.objToObj(teacherPoResp, UserTeacherInfoBoResp.class);
                return userTeacherInfoBoResp;
            }

        }else if(role.equals(RoleEnum.ROLESTUDENT.getRoleId())){
            //查询学生
            StudentPoReq studentPoReq = new StudentPoReq();
            studentPoReq.setStudentId(accountId);
            StudentPoResp studentPoResp = studentDao.getStudentByAccountId(userIdPoReq);
            log.info("根据账户编号"+accountId+"查询的学生信息为："+studentPoResp);
            if(studentPoResp != null){
                UserTeacherInfoBoResp userTeacherInfoBoResp=  CommonUtil.objToObj(studentPoResp, UserTeacherInfoBoResp.class);
                return userTeacherInfoBoResp;
            }

        }

        return null;

    }

    /**
     * 检查用户名和邮箱是否一致
     *  user 对象的accountId为空,说明不一致,有值说明一致
     * @param usernameAndEmailBoReq
     * @return
     */
    @Override
    public UserAccountIdBoResp checkUserNameAndEmail(UsernameAndEmailBoReq usernameAndEmailBoReq) throws SQLException {
        UserAccountIdBoResp user =  new UserAccountIdBoResp();
        //根据邮箱找到当前用户的角色和账户编号
        UserAccountIdAndRolePoResp userAccountIdAndRolePoResp =
                userDao.getUserAccountIdAndRoleByEmail(usernameAndEmailBoReq.getEmail());

        //根据角色和账户编号匹配老师和学生的姓名是否正确
        if(userAccountIdAndRolePoResp != null){
            //判断是老师还是学生
            if("1".equals(userAccountIdAndRolePoResp.getRole())){
                //是老师
                String teacherNameByAccountId = teacherDao.getTeacherNameByAccountId(userAccountIdAndRolePoResp.getAccountId());
                if(teacherNameByAccountId.equals(usernameAndEmailBoReq.getUserName())){
                    user.setAccountId(userAccountIdAndRolePoResp.getAccountId());
                }
            }else {
                //是学生
                String studentName = studentDao.getStudentNameByAccountId(userAccountIdAndRolePoResp.getAccountId());
                if(studentName.equals(usernameAndEmailBoReq.getUserName())){
                    user.setAccountId(userAccountIdAndRolePoResp.getAccountId());
                }

            }
        }
        return user;
    }

    /**
     * 修改密码
     *
     * @param userAccountIdAndPasswordBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp updateUserPassword(UserAccountIdAndPasswordBoReq userAccountIdAndPasswordBoReq) throws SQLException {
        UpdateUserPwdPoReq userPwdPoReq = CommonUtil.objToObj(userAccountIdAndPasswordBoReq, UpdateUserPwdPoReq.class);
        CommonPoResp commonPoResp=new CommonPoResp();
        int i = userDao.updatePassword(userPwdPoReq);
        CommonUtil.printToPoResult(commonPoResp,i);
        return CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
    }
}
